// #ifndef VUE3
import Vue from 'vue'
import App from './App'
import { createSSRApp } from 'vue'
import store from './store/index.js'
import uView from "uview-ui";
Vue.use(uView);

// 网络请求
import {
	$http
} from '@escook/request-miniprogram'

uni.$http = $http

$http.baseUrl = 'http://localhost:9999'
// $http.baseUrl = 'http://8.130.153.236:9999'
uni.$baseUrl = $http.baseUrl



// 递归把对象/数组里所有 Date 或日期字符串转成 ISO 字符串
function replaceDateToISO(obj) {
  if (!obj || typeof obj !== 'object') return
  for (const key in obj) {
    const val = obj[key]
    if (val instanceof Date) {
      // 如果是 Date 实例
      obj[key] = formatISO(val)
    } else if (typeof val === 'string' && /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/.test(val)) {
      // 如果是 "yyyy-MM-dd HH:mm:ss" 的字符串
      obj[key] = val.replace(' ', 'T')
    } else if (Array.isArray(val)) {
      val.forEach(replaceDateToISO)
    } else if (typeof val === 'object') {
      replaceDateToISO(val)
    }
  }
}

function formatISO(date) {
  const y = date.getFullYear()
  const m = String(date.getMonth() + 1).padStart(2, '0')
  const d = String(date.getDate()).padStart(2, '0')
  const H = String(date.getHours()).padStart(2, '0')
  const M = String(date.getMinutes()).padStart(2, '0')
  const S = String(date.getSeconds()).padStart(2, '0')
  return `${y}-${m}-${d}T${H}:${M}:${S}`
}

// 请求拦截器
$http.beforeRequest = function(options) {
	uni.showLoading({
		title: '数据加载中...'
	})	
	
	const authorization = uni.getStorageSync('authorization');
	$http.header = {
		"Authorization": authorization,
	}
	
 // 2. 统一把 yyyy-MM-dd HH:mm:ss 变成 yyyy-MM-ddTHH:mm:ss
  if (options.data) {
	replaceDateToISO(options.data);
  }
	
}

// 响应拦截器
$http.afterRequest = function(options) {
	if(options.data.code !== 20000){
		uni.showLoading({
			title: options.data.message,
			 mask: true // 可选：防止用户操作
		})	
	}
	setTimeout(() => {
	      uni.hideLoading();
	}, 500);
}



Vue.config.productionTip = false


App.mpType = 'app'

const app = new Vue({
	...App,
	store
})
app.$mount()
// #endif



// #ifdef VUE3
import {
	createSSRApp
} from 'vue'
import App from './App.vue'
export function createApp() {
	const app = createSSRApp(App)
	return {
		app,
		
	}
}
// #endif

